package springboot.study.project.service.impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springboot.study.project.dto.StudentDto;
import springboot.study.project.mapper.*;
import springboot.study.project.model.*;
import springboot.study.project.service.StudentService;
import springboot.study.project.util.common.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: StudentServiceImpl
 * @Description: 学生相关接口实现类
 * @Author: liu-hao
 * @Date: 2021-04-07 23:24
 * @Version: 1.0
 **/
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    MajorMapper majorMapper;
    @Autowired
    ClazzMapper clazzMapper;
    @Autowired
    DormitoryMapper dormitoryMapper;

    @Override
    @Transactional
    public boolean addStudentInfo(Student student) {
        if (student != null && StringUtil.isNotBlank(student.getStuId()) && StringUtil.isNotBlank(student.getStuName()) && StringUtil.isNotBlank(student.getDormitoryId()) && StringUtil.isNotBlank(student.getStuId()) && StringUtil.isNotBlank(student.getClazzId())) {
            StudentExample example = new StudentExample();
            example.createCriteria().andStuIdEqualTo(student.getStuId());
            int count = studentMapper.countByExample(example);
            if (count > 0) {
                return false;
            }
            student.setGmtCreate(System.currentTimeMillis());
            student.setGmtModify(student.getGmtCreate());
            int insertCount = studentMapper.insert(student);
            // 添加用户
            if (insertCount > 0) {
                User user = new User();
                user.setUserName(student.getStuId());
                user.setPassword("123");// 默认初始密码
                user.setRealName(student.getStuName());
                user.setPhone(student.getPhoneNumber());
                user.setRole("4"); // 1-超级管理员 2-老师 3-宿舍管理员 4-学生
                user.setRemark("该用户是学生");
                user.setGmtCreate(student.getGmtCreate());
                user.setGmtModify(user.getGmtCreate());
                userMapper.insert(user);
            }
            return insertCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean removeStudentInfoById(String stuId) {
        if (StringUtil.isNotBlank(stuId)) {
            int deleteCount = studentMapper.deleteByPrimaryKey(stuId);
            return deleteCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public boolean modifyStuInfo(Student student) {
        if (student != null && StringUtil.isNotBlank(student.getStuId())) {
            student.setGmtModify(System.currentTimeMillis());
            int updateCount = studentMapper.updateByPrimaryKeySelective(student);
            return updateCount > 0 ? true : false;
        }
        return false;
    }

    @Override
    public StudentDto getStuInfoById(String stuId) {
        if (StringUtil.isNotBlank(stuId)) {
            Student student = studentMapper.selectByPrimaryKey(stuId);
            if (student != null) {
                StudentDto studentDTO = new StudentDto();
                // 将两个对象中相同字段的属性值自动拷贝到另一个实体对象中去
                BeanUtils.copyProperties(student, studentDTO);
                Dormitory dormitory = dormitoryMapper.selectByPrimaryKey(student.getDormitoryId());
                Clazz clazz = clazzMapper.selectByPrimaryKey(student.getClazzId());
                Major major = majorMapper.selectByPrimaryKey(clazz != null ? clazz.getMajorId() : "");
                Department department = departmentMapper.selectByPrimaryKey(major != null ? major.getDeptId() : "");
                if (dormitory == null || clazz == null || major == null || department == null) {
                    return null;
                }
                studentDTO.setDormitoryName(dormitory.getDormitoryName());
                studentDTO.setClazzName(clazz.getClazzName());
                studentDTO.setMajorName(major.getMajorName());
                studentDTO.setMajorId(major.getMajorId());
                studentDTO.setDeptId(department.getDeptId());
                studentDTO.setDeptName(department.getDeptName());

                return studentDTO;
            }
        }
        return null;
    }

    @Override
    public Map<String, Object> getStuInfo(String stuId, String stuName, String clazzId, int offset, int pageSize) {
        // 拼接查询条件
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(stuId)) {
            criteria.andStuIdEqualTo(stuId);
        }
        if (StringUtil.isNotBlank(stuName)) {
            criteria.andStuNameLike("%" + stuName + "%");
        }
        if (StringUtil.isNotBlank(clazzId)) {
            criteria.andClazzIdEqualTo(clazzId);
        }
        // 查询符合条件学生数量
        int totalCount = studentMapper.countByExample(example);
        // 查询符合条件的分页学生信息
        List<Student> students = studentMapper.selectByExampleWithRowbounds(example, new RowBounds(offset, pageSize));

        // 填充学生属性
        List<StudentDto> questionDtoList = new ArrayList<>();
        for (Student student : students) {
            StudentDto studentDTO = new StudentDto();
            // 将两个对象中相同字段的属性值自动拷贝到另一个实体对象中去
            BeanUtils.copyProperties(student, studentDTO);
            Dormitory dormitory = dormitoryMapper.selectByPrimaryKey(student.getDormitoryId());
            Clazz clazz = clazzMapper.selectByPrimaryKey(student.getClazzId());
            Major major = majorMapper.selectByPrimaryKey(clazz != null ? clazz.getMajorId() : "");
            Department department = departmentMapper.selectByPrimaryKey(major != null ? major.getDeptId() : "");
            if (dormitory == null || clazz == null || major == null || department == null) {
                continue;
            }
            studentDTO.setDormitoryName(dormitory.getDormitoryName());
            studentDTO.setClazzName(clazz.getClazzName());
            studentDTO.setMajorName(major.getMajorName());
            studentDTO.setMajorId(major.getMajorId());
            studentDTO.setDeptId(department.getDeptId());
            studentDTO.setDeptName(department.getDeptName());
            questionDtoList.add(studentDTO);
        }

        // 定义返回Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("msg", 0);
        resultMap.put("count", totalCount);
        resultMap.put("data", questionDtoList);
        return resultMap;
    }

    @Override
    public boolean checkStuIsExisted(String stuId) {
        if (StringUtil.isBlank(stuId)) {
            return false;
        }
        StudentExample example = new StudentExample();
        example.createCriteria().andStuIdEqualTo(stuId);
        int count = studentMapper.countByExample(example);

        return count > 0 ? true : false;
    }
}
